<?php
/*
 * @Author: CYH
 * @Date: 2020-10-31 10:25:06
 * @LastEditTime: 2021-05-08 16:43:36
 * @LastEditors: Please set LastEditors
 * @Description: 区域管理control类
 * @FilePath: \application\controllers\Area\Area.php
 */
// 指定允许其他域名访问  
header('Access-Control-Allow-Origin:*');
// 响应类型  
header('Access-Control-Allow-Methods:POST');
// 响应头设置  
header('Access-Control-Allow-Headers:x-requested-with,content-type:application/json,charset:utf-8');
require_once 'PHPExcel-1.8\Classes\PHPExcel.php';
require_once 'PHPExcel-1.8\Classes\PHPExcel\IOFactory.php';
// require_once 'PHPExcel\Classes\PHPExcel\Reader\Excel5.php';
require_once 'PHPExcel-1.8\Classes\PHPExcel\Reader\Excel2007.php';

//区域管理control类
class Area extends CI_Controller
{

    //构造方法
    public function __construct()
    {

        parent::__construct();
        $this->load->model('Dorm/Dorm_manage');
        $this->load->model('Area/Area_manage');
        $this->load->helper('url_helper');
        $this->load->library('session');
        // $this->load->helper('date');
        $this->load->helper(array('form', 'url', 'date'));
    }

    /**
     * @description: 区域管理搜索方法
     * @param {area_number, area_name, limit}
     * @return json:array{'res'=>'','msg'=>'','data'=>''}
     */    
    public function area_select()
    {
        $area_number = $_POST['area_number']; //区域编号
        $area_name = $_POST['area_name'];     //区域名称
        $limit = $_POST['page'];              //页数

        // $area_number = "";
        // $area_name = "";
        // $limit = "";

        $count = $this->Area_manage->area_select_all(
            $area_number,
            $area_name,
            $limit
        );

        $result_json = $this->Area_manage->area_select(
            $area_number,
            $area_name,
            $limit,
            $count
        );

        echo $result_json;

        return 0;
    }

    /**
     * @description: 区域新增方法 
     * @param {area_name, area_location, remarks, area_createman}
     * @return json:array{'res'=>'','msg'=>'','data'=>''}
     */    
    public function area_add()
    {
        $area_name = $_POST['area_name'];           //区域名称
        $area_number = $_POST['area_number'];       //区域编号
        $area_location = $_POST['area_location'];   //区域位置
        $remarks = $_POST['remarks'];               //区域备注
        $area_createman = $_POST['area_createman']; //区域创建人

        // $area_name = "测试2"; 
        // $area_number = "1585";
        // $area_location = "高松南"; 
        // $remarks = "测试"; 
        // $area_createman = "yst"; 

        $result_json = $this->Area_manage->area_add(
            $area_name,
            $area_number,
            $area_location,
            $remarks,
            $area_createman
        );

        echo $result_json;

        return 0;
    }

    /**
     * @description: 区域删除方法
     * @param {area_number}
     * @return json:array{'res'=>'','msg'=>'','data'=>''}
     */    
    public function area_delete()
    {

        $area_number = $_POST['area_number']; //区域编码

        // $area_number = "334";

        $result_json = $this->Area_manage->area_delete(
            $area_number
        );

        echo $result_json;

        return 0;
    }

    /**
     * @description: 区域修改方法
     * @param {area_name, area_number, area_location, area_createman, area_cdate, remarks}
     * @return json:array{'res'=>'','msg'=>'','data'=>''}
     */    
    public function area_update()
    {

        $area_name = $_POST['area_name'];           //区域名称
        $area_number = $_POST['area_number'];       //区域编号
        $area_location = $_POST['area_location'];   //区域位置
        $area_createman = $_POST['area_createman']; //区域创建人
        $area_cdate = $_POST['area_cdate'];         //区域创建日期
        $remarks = $_POST['remarks'];               //区域备注

        // $area_name = "334号区域"; 
        // $area_number = "333"; 
        // $area_location = ""; 
        // $area_createman = ""; 
        // $area_cdate = ""; 
        // $remarks = ""; 

        $result_json = $this->Area_manage->area_update(
            $area_name,
            $area_number,
            $area_location,
            $area_createman,
            $area_cdate,
            $remarks
        );

        echo $result_json;

        return 0;
    }
    
    /**
     * @description: 区域excel导出
     * @param {*}
     * @return {*}
     */    
    public function excel_area()
    {        
        // 创建一个处理对象实例   
        $objExcel = new \PHPExcel();  

        // 创建文件格式写入对象实例, uncomment   
        $objProps = $objExcel->getProperties();  
        //设置创建者  TODO:
        $objProps->setCreator('admin');
        //描述  
        // $objProps->setDescription("入住信息汇总");  
        $objActSheet = $objExcel->getActiveSheet();
        //单元格赋值    
        $objActSheet->setCellValue('A1','区域名称');
        $objActSheet->setCellValue('B1','区域编号');
        $objActSheet->setCellValue('C1','地理位置');
        $objActSheet->setCellValue('D1','创建人员');
        $objActSheet->setCellValue('E1','创建时间');
        $objActSheet->setCellValue('F1','备注');

        $num = 2;

        $data = $this->Area_manage->excel_area();
        $data = json_decode($data,true);

        

        for ($i=0; $i < count($data['msg']); $i++) 
        {
            $objActSheet->setCellValueExplicit('A' . $num ,$data['msg'][$i]['area_name'] ,PHPExcel_Cell_DataType::TYPE_STRING);         //区域名称
            $objActSheet->setCellValueExplicit('B' . $num ,$data['msg'][$i]['area_number'] ,PHPExcel_Cell_DataType::TYPE_STRING);       //区域编号
            $objActSheet->setCellValueExplicit('C' . $num ,$data['msg'][$i]['area_location'] ,PHPExcel_Cell_DataType::TYPE_STRING);     //地理位置
            $objActSheet->setCellValueExplicit('D' . $num ,$data['msg'][$i]['area_createman'] ,PHPExcel_Cell_DataType::TYPE_STRING);    //创建人员
            $objActSheet->setCellValueExplicit('E' . $num ,$data['msg'][$i]['area_cdate'] ,PHPExcel_Cell_DataType::TYPE_STRING);        //创建时间 
            $objActSheet->setCellValueExplicit('F' . $num ,$data['msg'][$i]['remarks'] ,PHPExcel_Cell_DataType::TYPE_STRING);           //备注
            //设置行的属性  
            $objActSheet->getRowDimension($num)->setRowHeight('20px');//设置行高  
            $num = $num + 1;
        }

        //设置列的属性  
        $objActSheet->getColumnDimension('A')->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("B")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("C")->setWidth(30); // 设置宽度  
        $objActSheet->getColumnDimension("D")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("E")->setWidth(20); // 设置宽度  
        $objActSheet->getColumnDimension("F")->setWidth(20); // 设置宽度 
        $outputFileName = iconv('UTF-8', 'gbk', '区域信息.xlsx');  
        
        //文件直接输出到浏览器  
        header('Pragma:public');  
        header('Expires:0');  
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');  
        header('Content-Type:application/force-download');  
        header('Content-Type:application/vnd.ms-excel');  
        header('Content-Type:application/octet-stream');  
        header('Content-Type:application/download');  
        header('Content-Disposition:attachment;filename=' . $outputFileName);  
        header('Content-Transfer-Encoding:binary'); 
        PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007')->save('php://output');
        
        //设置对齐方式      
        $objStyleA3 = $objActSheet->getStyle('A3');  
        $objAlignA3 = $objStyleA3->getAlignment();  
        $objAlignA3->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);  
        $objAlignA3->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

        $objExcel = null;
    }
}
